/**
 * 页面初始化
 */
$(function () {
    //异步请求产品类型
    $.ajax({
        type: 'get',
        url: '/productMenu',
        data: {},
        success: function (data) {
            // console.log(data.list);
            if (data.message == "success") {
                if (data.total > 0) {
                    let num = 0;
                    $(data.list).each(function (index,item) {
                        num++;
                        let productNav = "<div >\n" +
                            "                    <a href=\"#\" onclick=\"findById("+item.id+",'/html/product_display.html')\">\n" +
                            "                        <span>"+item.name+"</span>\n" +
                            "                    </a>\n"+
                            "<div class=\"dropdown_"+num+"column\">\n" +
                            "                        <div class=\"col_1\">\n" +
                            "                            <ul class=\"simple\">";
                        let id = item.id;
                        if (item.productMenus.length > 0) {
                            $(item.productMenus).each(function (index,item) {
                                if (id == item.parentId) {
                                    productNav += "<li><a href=\"#\" onclick=\"findById("+item.id+",'/html/product_display.html')\">"+item.name+"</a></li>";
                                }
                            });
                        }
                        productNav += "</ul>\n" +
                            "                        </div>"+
                            "                    </div>\n" +
                            "                </div>";
                        $("#product-nav").append(productNav);
                    })
                }
                return false;
            }
            layer.msg('查询失败，请稍后重试！', {icon: 2, time: 1200});
            return false;
        },
        error: function () {
            layui.layer.msg('请求接口异常,请刷新重试！',{icon:0,time:1300});
            return false;
        }
    })

//  查询产品详情
    findByIdDetail();

})

function findByIdDetail() {
    var id = sessionStorage.getItem("id");
    if (id == null) {
        $("#detail-product").html("暂无详情,请重新选择产品！");
        $("#page-product").html("");
        return false;
    }
    //异步请求产品类型
    $.ajax({
        type: 'get',
        url: '/ProductFindById',
        data: {'id':id},
        success: function (data) {
            // console.log(data);
            if (data.message == "success") {
                if (data.productList.length == 0) {
                    $("#detail-product").html("暂无详情,请重新选择产品！");
                    $("#page-product").html("");
                    return false;
                }
                if (data.productList.length == 2) {
                    // TRUE 则当前产品为详情产品
                    if (data.productList[0].id == id) {
                        let {img,name,detail,marketPrice,platformPrice} = data.productList[0];
                        $("#details-img").html("<img src='"+img+"' >");
                        $("#details-title").html(name);
                        $("#details-marketPrice").html(marketPrice);
                        $("#details-platformPrice").html(platformPrice);
                        $("#details-name").html(name);
                        $("#product-detail").html(detail);
                        let pageProduct = "<div>\n" +
                            "                    <a href=\"javascript:void(0);\" >\n" +
                            "                        上一个：<span>无</span>\n" +
                            "                    </a>\n" +
                            "                </div>\n" +
                            "                <div>\n" +
                            "                    <a href=\"javascript:void(0);\" onclick=\"findById(" + data.productList[1].id + ",'/html/product_details.html')\" >\n" +
                            "                        下一个：<span>" + data.productList[1].name + "</span>\n" +
                            "                    </a>\n" +
                            "                </div>";
                        $("#page-product").html(pageProduct);
                        return false;
                    } else {
                        let {img,name,detail,marketPrice,platformPrice} = data.productList[1];
                        $("#details-img").html("<img src='"+img+"' >");
                        $("#details-title").html(name);
                        $("#details-marketPrice").html(marketPrice);
                        $("#details-platformPrice").html(platformPrice);
                        $("#details-name").html(name);
                        $("#product-detail").html(detail);
                        let pageProduct = "<div>\n" +
                            "                    <a href=\"javascript:void(0);\" onclick=\"findById(" + data.productList[0].id + ",'/html/product_details.html')\"  >\n" +
                            "                        上一个：<span>" + data.productList[0].name + "</span>\n" +
                            "                    </a>\n" +
                            "                </div>\n" +
                            "                <div>\n" +
                            "                    <a href=\"javascript:void(0);\" >\n" +
                            "                        下一个：<span>无</span>\n" +
                            "                    </a>\n" +
                            "                </div>";
                        $("#page-product").html(pageProduct);
                        return false;
                    }
                } else {
                    let {img,name,detail,marketPrice,platformPrice} = data.productList[1];
                    $("#details-img").html("<img src='"+img+"' >");
                    $("#details-title").html(name);
                    $("#details-marketPrice").html(marketPrice);
                    $("#details-platformPrice").html(platformPrice);
                    $("#details-name").html(name);
                    $("#product-detail").html(detail);
                    let pageProduct = "<div>\n" +
                        "                    <a href=\"javascript:void(0);\" onclick=\"findById(" + data.productList[0].id + ",'/html/product_details.html')\" >\n" +
                        "                        上一个：<span>"+data.productList[0].name+"</span>\n" +
                        "                    </a>\n" +
                        "                </div>\n" +
                        "                <div>\n" +
                        "                    <a href=\"javascript:void(0);\" onclick=\"findById(" + data.productList[2].id + ",'/html/product_details.html')\" >\n" +
                        "                        下一个：<span>" + data.productList[2].name + "</span>\n" +
                        "                    </a>\n" +
                        "                </div>";
                    $("#page-product").html(pageProduct);
                    return false;
                }
            }
            layer.msg('查询失败，请稍后重试！', {icon: 2, time: 1200});
            return false;
        },
        error: function () {
            layui.layer.msg('请求接口异常,请刷新重试！',{icon:0,time:1300});
            return false;
        }
    })


}